﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PagedList;
using System.Data.Objects;

namespace DLL
{
    public class InProductRepo
    {
        private string m_ConnectionString;

        public InProductRepo()
        {
            m_ConnectionString = "name=RetailManagementEntities";
        }

        public InProductRepo(string connectionString)
        {
            m_ConnectionString = connectionString;
        }

        #region Paper
        public List<pInProdPaper_Get_Result> GetInProdPaper(DateTime? fromDate = null, DateTime? toDate = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                var historyPaper = dbContext.pInProdPaper_Get(fromDate, toDate).ToList();
                return historyPaper;
            }
        }

        public void DeleteInProdPaper(int id)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                dbContext.pInProdPaper_Delete(id);
            }
        }

        public void UpdateInPaper(InProdPaper paper)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                dbContext.pInProdPaper_Update(paper.Id, paper.Date);
            }
        }

        public void AddInPaper(InProdPaper paper)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                dbContext.InProdPapers.Add(paper);
                dbContext.SaveChanges();
            }
        }
        #endregion Paper

        #region Detail
        public List<pInProdDetail_Get_Result> GetInProdDetails(int paperId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                return dbContext.pInProdDetail_Get(paperId).ToList();
            }
        }

        public pInProdDetail_Add_Result AddInProdDetail(int amount, int productId, int price, int inProdPaperId = 0, int createBy = 0, DateTime? date = null )
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                return dbContext.pInProdDetail_Add(productId, amount, price, inProdPaperId, createBy, date).FirstOrDefault();
            }
        }

        public int UpdateInProdDetail(int id, int? amount = null, int? inputPrice = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                var returnValue = dbContext.pInProdDetail_Update(id, amount, inputPrice);
                return returnValue.FirstOrDefault().Value;
            }
        }

        public int DeleteInProdDetail(int id)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                return dbContext.pInProdDetail_Delete(id).FirstOrDefault().Value;
            }
        }

        public List<pInProdHistory_Get_Result> GetInProductHistory(int productId, DateTime? fromDate = null, DateTime? toDate = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities(m_ConnectionString))
            {
                var histories = dbContext.pInProdHistory_Get(productId, fromDate, toDate).ToList();
                return histories;
            }
        }
        #endregion Detail
    }
}
